package com.lgr.store.utils;

import com.lgr.store.vo.EmpVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import sun.misc.BASE64Decoder;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class ContextUtils {

	public final static String[] ITEMTYPE={"OV","OR","MI","MR","BS","ZI","ZO","PP","PF","FI","FO"};
	public final static String[] MATERIALATTR={"原料","辅料","中间件","半成品","副产","成品"};
	public static EmpVo getUserInfo(){

		ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		HttpServletRequest request = requestAttributes.getRequest();
		HttpSession session = request.getSession();
		return (EmpVo) session.getAttribute("userinfo");
	}
	//日期转换
	public Date toDate(Object o) {
//		System.out.println("o="+o.toString());
		Date date = null;
		if (o == null|| o.toString().equals(""))
			return null;
		try {
			date = new Date(o.toString().trim().replace("-", "/"));
		} catch (Exception e) {
			date = new Date(new Integer(o.toString().trim()).longValue());
		}
		return date;
	}
	//double类型转换
	public Double toDouble(Object o) {
		Double b = 0.0;
		if (o!=null && !o.toString().trim().equals("")) {
			b = Double.parseDouble(o.toString().trim());
		}
		return b;
	}
	public Float toFloat(Object o) {
		Float b = 0.0f;
		if (o!=null && !o.toString().trim().equals("")) {
			b = Float.parseFloat(o.toString().trim());
		}
		return b;
	}
	//double类型转换为字符串
	public static String doubleToStr(double d){
		java.text.NumberFormat nf = java.text.NumberFormat.getInstance();
		nf.setGroupingUsed(false);
		return nf.format(d);
	}
	public static String floatToStr(float d){
		java.text.NumberFormat nf = java.text.NumberFormat.getInstance();
		nf.setGroupingUsed(false);
		return nf.format(d);
	}
	//上传文件重命名函数
	public static String getFilename(){
		Date d = new Date();
		int year = d.getYear()+1900;
		int month = d.getMonth()+1;
		int day = d.getDate();
		int hour = d.getHours();
		int minute = d.getMinutes();
		int second = d.getSeconds();
		String sdate = year+"";
		if(month<10)
			sdate += "0"+month;
		else
			sdate += month;
		if(day<10){
			sdate += "0"+day;
		}else{
			sdate += day;
		}
		if(hour<10){
			sdate += "0"+hour;
		}else{
			sdate += hour;
		}
		if(minute<10){
			sdate += "0"+minute;
		}else{
			sdate += minute;
		}
		if(second<10){
			sdate += "0"+second;
		}else{
			sdate += second;
		}
		return sdate;
	}

	//date类型转换为格式化后的字符串类型
	public static String dateToStrLong(Date d){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(d);
	}
	//date类型转换为格式化后的字符串类型
	public static String dateToStrShort(Date d){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(d);
	}
	public static String dateToStrShort1(Date d){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddmmssms");
		return sdf.format(d);
	}
	public static Date StringDateShort(String str){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date=null;
		try {
			date =  sdf.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}
	//get方式提交数据，进行乱码处理
	public String toStr(String str){
		try{
			str = new String(str.getBytes("iso-8859-1"),"utf-8");
		}catch(Exception e){
			e.printStackTrace();
		}
		return str;
	}
	public static void decodeBase64ToImage(String base64, String path,
										   String imgName) {
		BASE64Decoder decoder = new BASE64Decoder();
		try {
			FileOutputStream write = new FileOutputStream(new File(path
					+ imgName));
			byte[] decoderBytes = decoder.decodeBuffer(base64);
			write.write(decoderBytes);
			write.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}



}
